#include "logger.h"

Logger * Logger::_pInstance = nullptr;
Category * Logger::_root = nullptr;
void Logger::warn(const char * file, int line, const char * func, const char * msg){
        string content = string(file) + to_string(line) + string(func) + string(msg);
        _root->warn(content);
}
void Logger::error(const char * file, int line, const char * func, const char * msg){
        string content = string(file) + to_string(line) + string(func) + string(msg);
        _root->error(content);
}
void Logger::debug(const char * file, int line, const char * func, const char * msg){
        string content = string(file) + to_string(line) + string(func) + string(msg);
        _root->debug(content);
}
void Logger::info(const char * file, int line, const char * func, const char * msg){
        string content = string(file) + to_string(line) + string(func) + string(msg);
        _root->info(content);
}

Logger * Logger::getInstance(){
    if(_pInstance == nullptr){
        _pInstance = new Logger();
    }
    return _pInstance;
}

void Logger::destroy(){
    if(_pInstance){
        delete _pInstance;
        _pInstance = nullptr;
    }
}
   
Logger::Logger(){
    OstreamAppender * ap1 = new OstreamAppender("console", &cout);
    PatternLayout * pl1 = new PatternLayout();
    pl1->setConversionPattern("%d %c [%p] %m%n");
    ap1->setLayout(pl1);

    FileAppender * ap2 = new FileAppender("file", "singleton.log");
    PatternLayout * pl2 = new PatternLayout();
    pl2->setConversionPattern("%d %c [%p] %m%n");
    ap2->setLayout(pl2);

    Category & root = Category::getRoot();
    root.addAppender(ap1);
    root.addAppender(ap2);
    root.setPriority(Priority::INFO);
    _root = &root;
}
Logger::~Logger(){
    Category::shutdown();
}
